package com.shawn.spring.boot.flowable.service.impl;

import com.shawn.spring.boot.flowable.bo.User;
import com.shawn.spring.boot.flowable.dao.UserDao;
import com.shawn.spring.boot.flowable.dao.UserRoleDao;
import com.shawn.spring.boot.flowable.entity.UserEntity;
import com.shawn.spring.boot.flowable.entity.UserRoleEntity;
import com.shawn.spring.boot.flowable.mapper.UserEntityBOMapper;
import com.shawn.spring.boot.flowable.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

  private final UserDao userDao;
  private final UserRoleDao userRoleDao;

  private final UserEntityBOMapper userEntityBOMapper;

  public UserServiceImpl(
      UserDao userDao, UserRoleDao userRoleDao, UserEntityBOMapper userEntityBOMapper) {
    this.userDao = userDao;
    this.userRoleDao = userRoleDao;
    this.userEntityBOMapper = userEntityBOMapper;
  }

  @Override
  public void registry(User user) {
    UserEntity entity = userEntityBOMapper.toUserEntity(user);
    int id = userDao.insert(entity);
    UserRoleEntity userRoleEntity =
        UserRoleEntity.builder().userId(id).roleId(user.getRoles().get(0).getId()).build();
    userRoleDao.insert(userRoleEntity);
  }
}
